package edu.gatech.ealf.ettplaf;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComponent;

import edu.gatech.ealf.ComponentAccessibilityConfiguration;
import edu.gatech.ealf.EasyAccessibilityLookAndFeel;

public class ETTModel {

    public String toolTipText;
    public String extendedToolTipText;
    public JComponent component;
    public ToolTipHandler toolTipHandler = null;
    public ExtendedToolTipHandler extendedToolTipHandler = null;
    public Container glass = null;
    private ETTHelper helper = null;
    private boolean extendedToolTipValid = false;
    private boolean toolTipValid = false;
    private ExtendedToolTipProvider ettProvider = null;
    private boolean needDelayedLoad = false;
    
    public ETTModel(JComponent component, ExtendedToolTipProvider ettProvider){
        this.component = component;
        this.ettProvider = ettProvider;
        String componentName = component.getName();
        if(componentName == null || componentName.equals("")){
        	needDelayedLoad = true;
        } else {
        	loadConfig(component, componentName);
        }
        toolTipHandler = new ToolTipHandler();
        extendedToolTipHandler = new ExtendedToolTipHandler();
        if(component.getRootPane() != null){
            setGlass((Container) component.getRootPane().getGlassPane());
        }
        helper = ETTHelper.getInstance();
    }

	private void loadConfig(JComponent component, String componentName) {
		needDelayedLoad = false;
		ComponentAccessibilityConfiguration config = EasyAccessibilityLookAndFeel.componentConfig.get(componentName);
        
        if(config == null){
            toolTipText = component.getToolTipText();
        } else {
            toolTipText = config.toolTipText;
            extendedToolTipText = config.extendedToolTipText;
        }
        if(toolTipText == null || toolTipText.equals("")){
            toolTipText = component.getToolTipText();
        }
        component.setToolTipText(null);
        
        if(toolTipText != null && !toolTipText.equals("")){
            toolTipValid = true;
        }
        
        if(extendedToolTipText != null && !extendedToolTipText.equals("")){
            extendedToolTipValid = true;
        }
        
        if(toolTipValid && extendedToolTipValid){
            extendedToolTipText = toolTipText + ": " + extendedToolTipText;
        }
	}
    
    public void destroy(){
        component.setToolTipText(toolTipText);
        glass = null;
        component = null;
    }
    
    private class ToolTipHandler implements ActionListener{

        public void actionPerformed(ActionEvent e) {
                        
            if(toolTipValid){
            	setGlass((Container)component.getRootPane().getGlassPane());
                helper.setToolTipText(glass, toolTipText, component);
            } else if(component.getToolTipText() != null){
            	setGlass((Container)component.getRootPane().getGlassPane());
                toolTipText = component.getToolTipText();
                component.setToolTipText(null);
                helper.setToolTipText(glass, toolTipText, component);
                toolTipValid = true;
            }
            ettProvider.stopToolTipTimer();
        }
        
    }
    
    private class ExtendedToolTipHandler implements ActionListener{

        public void actionPerformed(ActionEvent e) {
        	
            if(extendedToolTipValid){
            	setGlass((Container)component.getRootPane().getGlassPane());
                helper.setToolTipText(glass, extendedToolTipText, component);
            } 
            ettProvider.stopExtendedToolTipTimer();
        }
        
    }
    
    public void lazyLoad(){
        if(needDelayedLoad){
            loadConfig(component, component.getName());
        }
    }
    
    public void setGlass(Container c){
        glass = c;
    }
    
}
